package com.example.webviewdemo.slice;

import com.example.webviewdemo.ExampleBrowserAgent;
import com.example.webviewdemo.ExampleWebAgent;
import com.example.webviewdemo.JSAncroid;
import com.example.webviewdemo.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.components.webengine.*;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.ToastDialog;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

public class MainAbilitySlice extends AbilitySlice implements Component.ClickedListener {
    // 定义日志标签
    private static final HiLogLabel LABEL = new HiLogLabel(HiLog.LOG_APP, 0x00201, "MY_TAG");
    private WebView webView;
    private DirectionalLayout web_parent;
    private Button btnLoadUrl,btnGoback,btnForward,btnLocal1,btnLocal2;
    private Button btnVue,btnJSToJAVA,btnNAtoJS;
    private TextField textField;

    private String virtual = "https://agree/";
    private String URLlocal1 = virtual + "index.html";
    private String URLlocal2 = "dataability://com.example.webviewdemo.ExampleDataAbility/resources/rawfile/www/test.html";
    private Navigator navigator;
    WebConfig webConfig;
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        initWeb1();

        navigator = webView.getNavigator();
        initView();
        webView.setWebAgent(new ExampleWebAgent(this));//通过setWebAgent方法设置自定义WebAgent对象，以观测页面状态变更等事件：
        webView.setBrowserAgent(new ExampleBrowserAgent(this));//通过setBrowserAgent方法设置自定义BrowserAgent对象，以观测JavaScript事件及通知等：
        webConfig = webView.getWebConfig();

        // 如果网页需要使用JavaScript，增加此行
        webConfig.setJavaScriptPermit(true);
        //注入回调对象到页面内容，并在页面中调用该对象
        webView.addJsCallback("JSAndroid", new JSAncroid(this));

    }

    public void initView(){
        textField = (TextField) findComponentById(ResourceTable.Id_text_field);
        btnLoadUrl = (Button) findComponentById(ResourceTable.Id_button1);//加载url
        btnLoadUrl.setClickedListener(this);
        btnGoback = (Button) findComponentById(ResourceTable.Id_button2);//go back
        btnGoback.setClickedListener(this);
        btnForward = (Button) findComponentById(ResourceTable.Id_button3);//go forward
        btnForward.setClickedListener(this);
        btnLocal1 = (Button) findComponentById(ResourceTable.Id_button4);
        btnLocal1.setClickedListener(this::onClick);
        btnLocal2 = (Button) findComponentById(ResourceTable.Id_button5);
        btnLocal2.setClickedListener(this::onClick);
        btnVue = (Button) findComponentById(ResourceTable.Id_button6);
        btnVue.setClickedListener(this::onClick);
        btnJSToJAVA = (Button) findComponentById(ResourceTable.Id_button7);
        btnJSToJAVA.setClickedListener(this::onClick);
        btnNAtoJS = (Button) findComponentById(ResourceTable.Id_button8);
        btnNAtoJS.setClickedListener(this::onClick);

    }
    //通过xml 布局
    public void initWeb1() {
        webView = (WebView) findComponentById(ResourceTable.Id_webview);
    }
    //通过代码生成
    public void initWeb2(){
        webView = new WebView(getContext());
        webView.setWidth(ComponentContainer.LayoutConfig.MATCH_PARENT);
        webView.setHeight(ComponentContainer.LayoutConfig.MATCH_PARENT);
        web_parent= (DirectionalLayout) findComponentById(ResourceTable.Id_web_parent);
        web_parent.addComponent(webView);
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    public void onClick(Component component) {
        int id = component.getId();
        switch (id){
            case ResourceTable.Id_button1:
                 webView.load(textField.getText());
                break;
            case ResourceTable.Id_button2://向后浏览
                if(navigator.canGoBack()){
                    navigator.goBack();
                }
                break;
            case ResourceTable.Id_button3://向前浏览
                if(navigator.canGoForward()){
                    navigator.goForward();
                }
                break;
            case ResourceTable.Id_button4://通过processResourceRequest方法访问文件
                webView.load(URLlocal1);
                break;
            case ResourceTable.Id_button5://通过Data Ability访问文件
                // 配置是否支持访问DataAbility资源，默认为true
                webConfig.setDataAbilityPermit(true);
                webView.load(URLlocal2);
                break;
            case ResourceTable.Id_button6://加载vue 的网页
               // webview加载Vue h5失败
                //"http://goosedev.mengqingyun.cn:50249/amapManager/#/login";//vue 写的网页
                //https://fee.net.chinamobile.com/unportal-app-view/404
                webView.load("https://amap.agreechina.com/amapManager/#/login");
                break;
            case ResourceTable.Id_button7://在应用内调用页面内的JavaScript方法
                    webView.executeJs("javascript:callFuncInWeb('测试')", new AsyncCallback<String>() {
                        @Override
                        public void onReceive(String msg) {
                            // 在此确认返回结果
//                            new ToastDialog(getContext())
//                                    .setText(msg)
//                                    .setAlignment(LayoutAlignment.CENTER)
//                                    .show();
                        }
                    });

                break;
           case ResourceTable.Id_button8://在应用内调用页面内的JavaScript方法
               webView.load("javascript:callFuncInWeb2('测试222')");
                break;
            default:
                break;
        }
    }
}
